package com.etoc.test.activiti.listener;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.TaskListener;

/**
 * 用户任务完成，发送微信
 * <一句话功能简述>
 * <功能详细描述>
 * 
 * @author  Administrator
 * @version  [版本号, 2016年12月20日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class TaskCompleteListener implements TaskListener {
	
	/**
	 * 注释内容
	 */
	private static final long serialVersionUID = -5343866677849340156L;
	private Expression task;
    private Expression content;

	@Override
	public void notify(DelegateTask delegateTask) {
		System.out.println("Task " + delegateTask.getName() + " " + delegateTask.getEventName() + ": " + task.getValue(delegateTask));
		String message = content.getValue(delegateTask).toString();
		String template = content.getValue(delegateTask).toString();
		String regexp = "\\$\\{(\\w+)\\}";
		Pattern pattern = Pattern.compile(regexp);
		Matcher matcher = pattern.matcher(template);
		while (matcher.find()) {
			String key = matcher.group(1);
			Object value = delegateTask.getVariable(key);
			if (value != null) {
				message = message.replaceAll("\\$\\{" + key + "\\}", value.toString());
			}
		}
		message = message.replaceAll("\\$\\{nodeName\\}", delegateTask.getName())
					.replaceAll("\\$\\{userId\\}", delegateTask.getAssignee());
		
		System.out.println(message);
		System.out.println("委托人" + delegateTask.getOwner() + ", 受理人: " + delegateTask.getAssignee());
	}

}
